/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ClassView.java
 *
 * Created on May 21, 2010, 9:44:22 AM
 */

package buggymastercode;

import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.MutableTreeNode;

/**
 *
 * @author jalvarez
 */
public class ClassView extends javax.swing.JDialog
                implements TreeSelectionListener {

    /** Creates new form ClassView */
    public ClassView(java.awt.Frame parent,
                        boolean modal) {
        super(parent, modal);
        initComponents();

        //Listen for when the selection changes.
        twClass.addTreeSelectionListener(this);
    }

    public void loadTree(ArrayList<SourceFile> collFiles, String vbpFile) {
        DefaultTreeModel model = (DefaultTreeModel)twClass.getModel();
        while (model.getChildCount(model.getRoot()) > 0) {
            model.removeNodeFromParent((MutableTreeNode)model.getChild(model.getRoot(), 0));
        }

        //Create the nodes.
        DefaultMutableTreeNode top = (DefaultMutableTreeNode)model.getRoot();
        Iterator itrFile = collFiles.iterator();
        while(itrFile.hasNext()) {
            SourceFile source = (SourceFile)itrFile.next();
            DefaultMutableTreeNode fileNode = new DefaultMutableTreeNode(source);
            top.add(fileNode);
            Iterator itrPublicFunctions = source.getPublicFunctions().iterator();
            while (itrPublicFunctions.hasNext()) {
                Function publicFunction = (Function)itrPublicFunctions.next();
                DefaultMutableTreeNode functionNode = new DefaultMutableTreeNode(publicFunction);
                fileNode.add(functionNode);
            }
            Iterator itrPrivateFunctions = source.getPrivateFunctions().iterator();
            while (itrPrivateFunctions.hasNext()) {
                Function privateFunction = (Function)itrPrivateFunctions.next();
                DefaultMutableTreeNode functionNode = new DefaultMutableTreeNode(privateFunction);
                fileNode.add(functionNode);
            }
        }
        model.reload();
    }

    /** Required by TreeSelectionListener interface. */
    public void valueChanged(TreeSelectionEvent e) {
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) twClass.getLastSelectedPathComponent();

        if (node == null) return;

        Object nodeInfo = node.getUserObject();
        if (node.isLeaf()) {
            Function function = (Function)nodeInfo;
            txCode.setText(function.getDeclarations());
        } else {
            txCode.setText("");
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jSplitPane1 = new javax.swing.JSplitPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        twClass = new javax.swing.JTree();
        jScrollPane2 = new javax.swing.JScrollPane();
        txCode = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(400, 300));
        setName("Form"); // NOI18N

        jSplitPane1.setName("jSplitPane1"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        twClass.setName("twClass"); // NOI18N
        jScrollPane1.setViewportView(twClass);

        jSplitPane1.setLeftComponent(jScrollPane1);

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        txCode.setColumns(20);
        txCode.setRows(5);
        txCode.setName("txCode"); // NOI18N
        jScrollPane2.setViewportView(txCode);

        jSplitPane1.setRightComponent(jScrollPane2);

        getContentPane().add(jSplitPane1, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JTree twClass;
    private javax.swing.JTextArea txCode;
    // End of variables declaration//GEN-END:variables

}
